﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class CMS_Role_GetEmpDepTree : System.Web.UI.Page
{
    Usas.BLL.Cms.UsasDB udb = new Usas.BLL.Cms.UsasDB();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            try
            {
                this.InitTree();
            }
            catch
            {
            }
        }
    }

    private void InitTree()
    {
        if (DptEmp.Nodes.Count > 0)
        {
            return;
        }
        //object[] myresult = Srvtools.CliUtils.CallMethod("Train", "GetEmpDepTrees", null);
        DataSet ds = udb.GetEmpDepTrees();
        if (ds != null)
        {
            DataTable dt = (DataTable)ds.Tables[0];

            DataView dv = dt.DefaultView;
            dv.RowFilter = "id=pid";

            for (int i = 0; i < dv.Count; i++)
            {
                TreeNode tn = new TreeNode(dv[i]["name"].ToString(), dv[i]["id"].ToString());
                //tn.ShowCheckBox = true;
                tn.Text = "<span name=\"ttv\"><input type='checkbox' id='tv\\" + dv[i]["id"].ToString() + "' name='" + dv[i]["name"].ToString() + "'>" + dv[i]["name"].ToString() + "</span>";
                //tn.SelectAction = TreeNodeSelectAction.Expand;
                //tn.Expanded = false;
                this.DptEmp.Nodes.Add(tn);

                this.TreeBind(tn, dt);
            }

        }
    }

    private void TreeBind(TreeNode tn, DataTable dt)
    {
        DataView dv = new DataView(dt);
        dv.RowFilter = "pid='" + tn.Value + "' and pid<>id";

        for (int i = 0; i < dv.Count; i++)
        {
            TreeNode treenode = new TreeNode(dv[i]["name"].ToString(), dv[i]["id"].ToString());
            //treenode.ShowCheckBox = true;
            treenode.Text = "<span name='ttv'><input type='checkbox' id='tv\\" + dv[i]["id"].ToString() + "' name='" + dv[i]["name"].ToString() + "''/>" + dv[i]["name"].ToString() + "</span>";
            //treenode.NavigateUrl = "javascript:void(0)";
            if (dv[i]["id"].ToString().IndexOf("D") != -1)
            {
                treenode.ImageUrl = "~/Image/dep.gif";
            }
            else
            {

                treenode.ImageUrl = "~/Image/job.gif";
            }

            tn.ChildNodes.Add(treenode);

            //if (treenode.Value.StartsWith("e"))
            //{
            //    string empId = treenode.Value.Remove(0, 1);
            //    treenode.Value = empId;
            //    treenode.ShowCheckBox = true;
            //    treenode.ImageUrl = "~/Image/job.gif";
            //}
            //else
            //{
            //    treenode.ImageUrl = "~/Image/dep.gif";
            //}



            DataView temdv = new DataView(dt);
            temdv.RowFilter = "pid='" + treenode.Value + "' and pid<>id";
            if (temdv.Count > 0)
                TreeBind(treenode, dt);
        }
    }


}
